package program;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 对文件进行复制
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2024-03-28  11:00
 */
public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件路径：");
        String srcPath = scanner.next();
        System.out.println("请输入要复制的文件路径：");
        String destPath = scanner.next();


        File srcFile = new File(srcPath);
        if(!srcFile.isFile()) {
            System.out.println("源文件目录有误");
            return;
        }
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()) {
            System.out.println("复制路径有误！");
            return;
        }

        try(InputStream inputStream = new FileInputStream(srcFile); OutputStream outputStream = new FileOutputStream(destFile)) {
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                outputStream.write(buffer,0,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
